﻿' LibWowAPI
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Drawing
Imports System.Linq
Imports System.Runtime.CompilerServices
Imports roncliProductions.LibWowAPI.Data
Imports roncliProductions.LibWowAPI.Data.CharacterClasses
Imports roncliProductions.LibWowAPI.Data.CharacterRaces
Imports roncliProductions.LibWowAPI.Enums

Namespace roncliProductions.LibWowAPI.Extensions

    Friend Module DateExtensions

        <Extension()> Friend Function ToCollection(Of T)(enumerable As IEnumerable(Of T)) As Collection(Of T)
            If enumerable Is Nothing Then
                Throw New ArgumentException("The enumeration is null.", "enumerable")
            End If

            Dim collection As New Collection(Of T)
            For Each obj As T In enumerable
                collection.Add(obj)
            Next
            Return collection
        End Function

        <Extension()> Friend Sub AddRange(Of T)(collection As Collection(Of T), enumerable As IEnumerable(Of T))
            If collection Is Nothing Then
                Throw New ArgumentException("The collection is null.", "collection")
            End If

            If enumerable Is Nothing Then
                Throw New ArgumentException("The enumeration is null.", "enumerable")
            End If

            For Each obj In enumerable
                collection.Add(obj)
            Next
        End Sub

        <Extension()> Friend Function BlizzardTimestampToUTC(lngTimestamp As Long) As Date
            Return New Date(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(lngTimestamp)
        End Function

        <Extension()> Friend Function ArgbHexToColor(strColor As String) As Color
            Return Color.FromArgb(Convert.ToInt32(strColor, 16))
        End Function

        <Extension()> Friend Function GetClass(intClassID As Integer) As [Class]
            Dim ccClasses As New CharacterClasses()
            ccClasses.Load()
            Dim varClass = ccClasses.Classes.Where(Function(c) c.ID = intClassID)
            If varClass.Count = 0 Then
                Return Nothing
            Else
                Return varClass.First()
            End If
        End Function

        <Extension()> Friend Function GetRace(intRaceID As Integer) As Race
            Dim crRaces As New CharacterRaces()
            crRaces.Load()
            Dim varRace = crRaces.Races.Where(Function(r) r.ID = intRaceID)
            If varRace.Count = 0 Then
                Return Nothing
            Else
                Return varRace.First()
            End If
        End Function

        <Extension()> Friend Function GetRealmType(strType As String) As RealmType
            Select Case strType
                Case "pve"
                    Return RealmType.PvE
                Case "pvp"
                    Return RealmType.PvP
                Case "rp"
                    Return RealmType.RP
                Case "rppvp"
                    Return RealmType.RPPvP
                Case Else
                    Return RealmType.Unknown
            End Select
        End Function

        <Extension()> Friend Function GetPowerType(strPowerType As String) As PowerType
            Select Case strPowerType
                Case "energy"
                    Return PowerType.Energy
                Case "focus"
                    Return PowerType.Focus
                Case "mana"
                    Return PowerType.Mana
                Case "rage"
                    Return PowerType.Rage
                Case "runic-power"
                    Return PowerType.RunicPower
                Case Else
                    Return PowerType.Unknown
            End Select
        End Function

        <Extension()> Friend Function GetSide(strSide As String) As Side
            Select Case strSide
                Case "alliance"
                    Return Side.Alliance
                Case "horde"
                    Return Side.Horde
                Case Else
                    Return Side.Unknown
            End Select
        End Function

        <Extension()> Friend Function GetItemClass(intItemClass As Integer) As ItemClasses.Class
            Dim icItemClasses = New ItemClasses.ItemClasses()
            icItemClasses.Load()
            Dim varItemClass = icItemClasses.Classes.Where(Function(c) c.Class = intItemClass)
            If varItemClass.Count = 0 Then
                Return Nothing
            Else
                Return varItemClass.First()
            End If
        End Function

        <Extension()> Friend Function GetAuctionTimeLeft(strTimeLeft As String) As AuctionTimeLeft
            Select Case strTimeLeft
                Case "SHORT"
                    Return AuctionTimeLeft.Short
                Case "MEDIUM"
                    Return AuctionTimeLeft.Medium
                Case "LONG"
                    Return AuctionTimeLeft.Long
                Case "VERY_LONG"
                    Return AuctionTimeLeft.VeryLong
                Case Else
                    Return AuctionTimeLeft.Unknown
            End Select
        End Function

        <Extension()> Friend Function GetRaces(intRaces As Collection(Of Integer)) As Collection(Of Race)
            If intRaces Is Nothing Then Return Nothing
            Dim crRaces As New CharacterRaces()
            crRaces.Load()
            Return (From r In crRaces.Races Where intRaces.Contains(r.ID)).ToCollection()
        End Function

        <Extension()> Friend Function GetClasses(intClasses As Collection(Of Integer)) As Collection(Of [Class])
            If intClasses Is Nothing Then Return Nothing
            Dim ccClasses As New CharacterClasses()
            ccClasses.Load()
            Return (From c In ccClasses.Classes Where intClasses.Contains(c.ID)).ToCollection()
        End Function

    End Module

End Namespace
